package router

import (
	"github.com/kataras/iris"
	"server/bitkop/app/api"
	"time"
	"github.com/kataras/iris/websocket"
	manager "server/bitkop/app/websocket"
)

func RegistRoutes(app *iris.Application) {
	setApiRoutes(app)
	setWebSocketRoutes(app)
}

// 中间键
func before(ctx iris.Context)  {
	ctx.Values().Set("ws", "")
	ctx.Next()
}

// HTTP 路由
func setApiRoutes(app *iris.Application) {
	app.UseGlobal(before)
	app.Handle("GET", "/", api.Index)
	app.Handle("GET", "/add_coin_pair", api.ServerAddCoinPair)
}

// web socket 注册
func setWebSocketRoutes(app *iris.Application) {

	ws := websocket.New(websocket.Config{
		MaxMessageSize: 		4096,
		ReadTimeout: 			0,
		WriteTimeout: 			60 * time.Second,
	})

	manager.NewSocketManager(ws).Init().Serv()

	app.Get("/ws/trade", ws.Handler())
}